package com.hgd.XML;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import com.hgd.ORM.Device;
import com.hgd.ORM.DispatchCfg;
import com.hgd.ORM.Domain;
import com.hgd.ORM.ManagementStation;
import com.hgd.ORM.Root;
import com.hgd.ORM.Task;
import com.hgd.ORM.TaskKind;
import com.thoughtworks.xstream.XStream;

public class TurnXMLToConfiguration {
	@SuppressWarnings("unchecked")
	public Set<ManagementStation> changeToBean() throws IOException{
		XStream stream = new XStream();
		
		stream.alias("Root", Root.class);//根标签
		stream.alias("ManagementStation", ManagementStation.class);//根标签
		stream.alias("DispatchCfg", DispatchCfg.class);//device标签
		
		/************* 设置类成员的别名 ***************/ 		
		stream.aliasField("ManagementStation", Root.class, "ManagementStation");//domain的device标签
		stream.aliasField("DispatchCfg", ManagementStation.class, "DispatchCfg");//domain的device标签
		stream.aliasField("r", DispatchCfg.class, "r");//device的tasks标签
		stream.aliasField("LOOP", DispatchCfg.class, "LOOP");//tasks的task标签
		/******* 设置类成员为xml一个元素上的属性 *******/
		stream.useAttributeFor(ManagementStation.class,"MSId");
		stream.useAttributeFor(ManagementStation.class,"MSName");
		stream.useAttributeFor(ManagementStation.class,"MSIp");
		stream.useAttributeFor(ManagementStation.class,"isInitialize");
			
		/************* 设置属性的别名 ***************/ 
		stream.aliasAttribute(ManagementStation.class, "MSId", "MSId");
		stream.aliasAttribute(ManagementStation.class, "MSName", "MSName");
		stream.aliasAttribute(ManagementStation.class, "MSIp", "MSIp");
		stream.aliasAttribute(ManagementStation.class, "isInitialize", "isInitialize");
		Root root = (Root) stream.fromXML(new FileInputStream("D:/DispatchCfg.xml"));
		return root.getManagementStations(); 
	}
	
	public static void main(String[] args) throws IOException {
		TurnXMLToConfiguration configuration = new TurnXMLToConfiguration();
		Set<ManagementStation> managementStations = configuration.changeToBean();
		for (Iterator iterator = managementStations.iterator(); iterator
				.hasNext();) {
			ManagementStation managementStation = (ManagementStation) iterator
					.next();
			System.out.println(managementStation.getMSName());
		}
	}
}
